package com.zcl.springboot.filter;

import javax.servlet.*;
import javax.servlet.FilterConfig;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 通过注解实现过滤器
 * WebFilter这个注解是Servlet3.0的规范，并不是Spring boot提供的。
 * 除了这个注解以外 我们还需在配置类中加另外一个注解：@ServletComponetScan，指定扫描的包。
 * @see com.zcl.springboot.SpringbootApplication
 */
@WebFilter(urlPatterns = "/*",filterName = "LogCostFilter2")
public class LogCostFilter2 implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        long start = System.currentTimeMillis();
        chain.doFilter(request,response);
        System.out.println("LogFilter2 Execute cost="+(System.currentTimeMillis() - start));
    }

    @Override
    public void destroy() {

    }
}
